package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    @Override
    public Map<String, Object> listReportData() {
        //构建一个map集合，通过键找值
        Map<String, Object> map = new HashMap<>();
        //设置每周时间范围  设置每月的时间范围
        //日期处理
        Calendar todays = Calendar.getInstance();
        //将时/分/秒/毫秒清零
        todays.set(Calendar.HOUR_OF_DAY, 0);
        todays.set(Calendar.MINUTE, 0);
        todays.set(Calendar.SECOND, 0);
        todays.set(Calendar.MILLISECOND, 0);
        //设置周一为每周第一天
        todays.setFirstDayOfWeek(Calendar.MONDAY);
        //本周第一天
        Calendar weekFirstDay = (Calendar) todays.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周最后一天
        Calendar weekLastDay = (Calendar) todays.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        //本月第一天
        Calendar monthFirstDay = (Calendar) todays.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) todays.clone();
        //获取最后一天的最大值
        int maximum = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, maximum);
        //定义日期存储格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取统计表生成得时间
        map.put("reportDate", dateFormat.format(todays.getTime()));
        //获取会员的数据，调用会员的数据层
        //本日新增会员数
        Long todayNewMember = memberDao.countFind(todays.getTime(), todays.getTime());
        map.put("todayNewMember", todayNewMember);
        //总会员数
        Integer totalMember = memberDao.findTotalMember(dateFormat.format(todays.getTime()));
        map.put("totalMember", totalMember);
        //本周新增会员数
        Long thisWeekNewMember = memberDao.countFind(weekFirstDay.getTime(), weekLastDay.getTime());
        map.put("thisWeekNewMember", thisWeekNewMember);
        //本月新增会员数
       Long thisMonthNewMember = memberDao.countFind(monthFirstDay.getTime(), monthLastDay.getTime());
        map.put("thisMonthNewMember", thisMonthNewMember);
        //获取预约的数据，调用预约列表数据层
        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderNumber(todays.getTime(), todays.getTime());
        map.put("todayOrderNumber", todayOrderNumber);
        //今日到诊数
        Integer todayVisitsNumber=orderDao.findVisitsNumber(todays.getTime(), todays.getTime(), Order.ORDERSTATUS_YES);
        map.put("todayVisitsNumber",todayVisitsNumber);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderNumber(weekFirstDay.getTime(),weekLastDay.getTime());
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsNumber(weekFirstDay.getTime(),weekLastDay.getTime(),Order.ORDERSTATUS_YES);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderNumber(monthFirstDay.getTime(),monthLastDay.getTime());
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        //本月到诊数
        Integer thisMonthVisitsNumber=orderDao.findVisitsNumber(monthFirstDay.getTime(),monthLastDay.getTime(),Order.ORDERSTATUS_YES);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        //根据预约信息分别获取前tpo个热门套餐的预约数量/根据套餐名称分类
        map.put("hotSetmeal",orderDao.findHotSetMeal(3));
        return map;
    }
}
